package com.ai.databao.web.controller.busi;

import java.util.HashMap;
import java.util.Map;

import com.ai.databao.common.constants.Constants;
import com.ai.databao.common.util.StringUtil;
import com.ai.databao.common.vo.comm.StaffInfoVo;
import com.ai.databao.web.system.util.StaffSessionUtil;
import org.apache.log4j.Logger;
import com.ai.databao.web.system.AppBaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by huangzh on 17/4/14.
 */
@Controller
@RequestMapping(value = "/login")
public class LoginController extends AppBaseController {
    private static final Logger log = Logger.getLogger(LoginController.class);
    /*
    * 功能描述:进入登录页面
    * 开发人员:huangzh5
    * */
    @RequestMapping()
    public String init() throws Exception{
        return "/login"; //返回页面
    }

    @RequestMapping(value="/check")
    @ResponseBody
    public Map logincheck(@RequestParam("username")String username, @RequestParam("password")String password, @RequestParam("capcode")String capcode) throws Exception{
        Map retMap = new HashMap();
        retMap.put(Constants.RetFlag.FLAG,Constants.RetFlag.ERROR);

        try {
            //校验传入参数是否为空
            if (StringUtil.isBlank(username)) {
                retMap.put(Constants.RetFlag.FLAG, Constants.RetFlag.ERROR);
                retMap.put(Constants.RetFlag.MSG, "用户名为空，请输入用户码!");
                return retMap;
            }
            if (StringUtil.isBlank(password)) {
                retMap.put(Constants.RetFlag.FLAG, Constants.RetFlag.ERROR);
                retMap.put(Constants.RetFlag.MSG, "密码为空，请输入密码!");
                return retMap;
            }
            if (StringUtil.isBlank(capcode)) {
                retMap.put(Constants.RetFlag.FLAG, Constants.RetFlag.ERROR);
                retMap.put(Constants.RetFlag.MSG, "验证码为空，请输入验证码!");
                return retMap;
            }

            if(!"admin".equals(username)){
                retMap.put(Constants.RetFlag.FLAG, Constants.RetFlag.ERROR);
                retMap.put(Constants.RetFlag.MSG, "用户名错误，请重新输入!");
                return retMap;
            }

            if(!"14e1b600b1fd579f47433b88e8d85291".equals(password)){
                retMap.put(Constants.RetFlag.FLAG, Constants.RetFlag.ERROR);
                retMap.put(Constants.RetFlag.MSG, "密码错误，请重新输入!");
                return retMap;
            }

            //校验验证码是否正确
            String sRandFont = (String) request.getSession().getAttribute("sRandFont");
            if (!capcode.equals(sRandFont)) {
                retMap.put(Constants.RetFlag.FLAG, Constants.RetFlag.ERROR);
                retMap.put(Constants.RetFlag.MSG, "验证码错误，请重新输入!");
                return retMap;
            }

            //将用户信息写入session
            StaffInfoVo staffInfo = new StaffInfoVo();
            staffInfo.setUserName(username);
            staffInfo.setUserId("1");
            StaffSessionUtil.setStaffInfo(request.getSession(), staffInfo);
            retMap.put(Constants.RetFlag.FLAG, Constants.RetFlag.SUCCESS);
            retMap.put(Constants.RetFlag.MSG, "登录成功!");

            return retMap;

        }catch (Exception e){
            log.error(e.toString());
            retMap.put(Constants.RetFlag.FLAG, Constants.RetFlag.ERROR);
            retMap.put(Constants.RetFlag.MSG, e.toString());
        }

        return retMap;
    }
}
